﻿using AutoMapper;
using MediatR;
using ProcurementManagement.Applcation.Command.ProcureApplyCommand;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;

namespace ProcurementManagement.Applcation.CommandHanlers.ProcureApplyCommandHandler
{
    /// <summary>
    /// 采购申请修改中介者
    /// </summary>
    public class ProcureApplyUpdCommandHandler:IRequestHandler<ProcureApplyUpdCommand,int>
    {
        private readonly lnterfaceProcureApplyRepository _procureApplyRepository;//采购申请表
        private readonly lnterfacePurchaseproductRepository _purchaseproductRepository;//采购明细表
        private readonly ISqlSugarClient db;
        private readonly IMapper _mapper;

        public ProcureApplyUpdCommandHandler(lnterfaceProcureApplyRepository procureApplyRepository, lnterfacePurchaseproductRepository purchaseproductRepository, ISqlSugarClient db, IMapper mapper)
        {
            _procureApplyRepository = procureApplyRepository;
            _purchaseproductRepository = purchaseproductRepository;
            this.db = db;
            _mapper = mapper;
        }

        public async Task<int> Handle(ProcureApplyUpdCommand request, CancellationToken cancellationToken)
        {
            //查询单据主题是否重复
            var res = (await _procureApplyRepository.GetAll()).First(x=>x.PAApplyMotif == request.dto.PAApplyMotif);
            if (res == null)
            {
                //开启事物
                db.Ado.BeginTran();
                try
                {
                    //修改采购申请表
                    var apply = (await _procureApplyRepository.GetAll()).First(x=>x.ProcureApplyId==request.dto.ProcureApplyId);
                    await _procureApplyRepository.UpdataAsync(_mapper.Map(request.dto,apply));
                    //先删除这个编号相同的采购明细
                    var detail = (await _purchaseproductRepository.GetAll()).Where(x=>x.PANumber==request.dto.PANumbers).ToList();
                    await _purchaseproductRepository.DelRange(detail);
                    //重新添加采购明细
                    foreach (var item in request.dto.list)
                    {
                        Purchaseproduct purchaseproduct = new Purchaseproduct
                        {
                            PurchaseproductId = item.PurchaseproductId,
                            PANumber = request.dto.PANumbers,
                            ProductId = item.ProductId,
                            VendorId = item.VendorId,
                            Num = item.Num,
                            Price = item.Price,
                            SumPrice = (item.Num * item.Price),
                            Datess = item.Datess,
                            Remark = item.Remark
                        };
                        await _purchaseproductRepository.AddAsync(purchaseproduct);
                    }
                    //提交事物
                    db.Ado.CommitTran();
                    return 1;
                }
                catch (Exception)
                {
                    //回滚事物
                    db.Ado.RollbackTran();
                    throw;
                }
            }
            else
            {
                //重复返回-1
                return -1;//主题名称重复不能修改
            }



        }
    }
}
